class code4 {
    //全排列 II
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;
    public List<List<Integer>> permuteUnique(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        check = new boolean[nums.length];
        Arrays.sort(nums);
        dfs(nums);
        return ret;
    }
    public void dfs(int[] nums) {
        if(path.size()==nums.length) {
            ret.add(new ArrayList<>(path));
            return;
        }

        for(int i=0;i<nums.length;i++) {
            if(check[i]==true||(i!=0&&nums[i]==nums[i-1]&&check[i-1]==false)) {
                continue;//剪枝
            } else {
                path.add(nums[i]);
                check[i] = true;
                dfs(nums);
                path.remove(path.size()-1);//回溯
                check[i] = false;
            }
        }
    }
}